package com.yuanchu.marketing.controller;

import com.yuanchu.common.utils.PageResult;
import com.yuanchu.common.utils.R;
import com.yuanchu.common.validator.ValidationGroups;
import com.yuanchu.marketing.dto.CouponParamsDto;
import com.yuanchu.marketing.dto.CouponReceiveDto;
import com.yuanchu.marketing.entity.YoshopCouponEntity;
import com.yuanchu.marketing.entity.YoshopUserCouponEntity;
import com.yuanchu.marketing.service.YoshopCouponService;
import com.yuanchu.marketing.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-02-09 10:29
 */
@Api(tags = "优惠券接口 管理")
@RestController
@RequestMapping("coupon")
public class CouponController {

    @Resource
    private YoshopCouponService couponService;

    @ApiOperation(value = "/api/coupon/detail/{couponId}", tags = "获取优惠券详情")
    @GetMapping("detail/{couponId}")
    public YoshopCouponEntity queryCouponById(@PathVariable("couponId") Integer couponId){
        YoshopCouponEntity coupon = couponService.queryCouponById(couponId);
        return coupon;
    }

    @ApiOperation(value = "/coupon/list", tags = "获取优惠券列表")
    @GetMapping("/list")
    public R queryCouponList(){
        Integer userId = SecurityUtil.getUserId();
        List<YoshopCouponEntity> coupons = couponService.queryCouponList(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("list", coupons);
        return R.ok().put("data", map);
    }
    @ApiOperation(value = "/coupon/list", tags = "获取优惠券列表")
    @GetMapping("/all")
    public R queryCouponAll(@RequestParam("page") Integer page, @RequestParam(value = "search", required = false) String search){
        PageResult<YoshopCouponEntity> coupons = couponService.queryCouponAll(page, search);
        return R.ok().putPage("data", coupons);
    }

    @GetMapping("detail")
    public R queryCouponDetail(@RequestParam("couponId") Integer couponId){
        YoshopCouponEntity coupon = couponService.queryCouponById(couponId);
        Map<String, Object> map = new HashMap<>();
        map.put("detail", coupon);
        return R.ok().put("data", map);
    }

    @PostMapping("edit")
    public R editCoupon(@RequestBody @Validated(ValidationGroups.Update.class) CouponParamsDto params){
        couponService.editCoupon(params);
        return R.ok();
    }

    @PostMapping("add")
    public R addCoupon(@RequestBody CouponParamsDto params){
        couponService.addCoupon(params);
        return R.ok();
    }

    @ApiOperation(value = "/coupon/list", tags = "获取优惠券列表记录")
    @GetMapping("/receive")
    public R queryCouponReceive( CouponReceiveDto params){
        PageResult<YoshopUserCouponEntity> coupons = couponService.queryCouponReceive(params);
        return R.ok().putPage("data", coupons);
    }

}
